package com.xgc.erp.info.entity.ctrl;

import com.jfinal.aop.Inject;
import com.jfinal.plugin.activerecord.Record;
import com.xgc.common.base.BaseController;
import com.xgc.common.config.Constan;
import com.xgc.common.config.WebContant;
import com.xgc.common.model.Entity;
import com.xgc.common.routes.ControllerBind;
import com.xgc.common.visit.Visitor;
import com.xgc.common.vo.Feedback;
import com.xgc.common.vo.TreeNode;
import com.xgc.erp.info.entity.service.EntityService;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;

@ControllerBind(path="/erp/info/entity")
public class EntityController extends BaseController {
    @Inject
    private EntityService service;

    public void index() {
        render("index.html");
    }

    public void list(){
        Record record = new Record();
        record.set("par_entity", getPara("par_id"));
        renderJson(service.queryForList(getParaToInt("pageNumber",1), getParaToInt("pageSize",10),record,"order by create_time desc"));
    }

    /**
     * 物品关系树
     *
     * @author QinHaiLin
     * @date 2018年10月9日
     */
    public void entityTree() {
        Collection<TreeNode> nodeList = this.service.getEntityTree(null);
        Collection<TreeNode> nodes = new ArrayList<TreeNode>();
        TreeNode node = new TreeNode();
        node.setId("");
        node.setText(WebContant.projectName);
        node.setChildren(nodeList);
        nodes.add(node);

        renderJson(nodes);
    }

    public void add() {
        String parEntity = getPara("par_entity");
        setAttr("par_entity", parEntity);
        Entity parent = (Entity) service.findById(parEntity);
        setAttr("parent", parent);
        render("add.html");
    }

    public void save() {
        Entity entity=getBean(Entity.class);
        Integer entityId = service.getPrimaryKey(Constan.SEQ_ENTITY);
        entity.setEntityId(entityId);
        Visitor vs = getVisitor();
        entity.setCreator(vs.getCode());
        entity.setCreateTime(new Date());
        boolean flag = service.isExit(service.getPK(),entityId.toString());
        if(flag){
            setException("主键已存在");
        }else {
            flag = service.save(entity);
            if(!flag) {
                setException("保存失败");
            }
        }
        render("add.html");
    }

    public void edit() throws UnsupportedEncodingException {
        Entity entity =(Entity) service.findById(URLDecoder.decode(getPara("primaryKey"),"utf-8"));
        Entity parent = (Entity) service.findById(entity.getParEntity().toString());
        setAttr("entity", entity);
        setAttr("parent", parent);
        render("edit.html");
    }

    public void update() {
        Entity entity=getBean(Entity.class);
        Visitor vs = getVisitor();
        entity.setModifier(vs.getCode());
        entity.setUpdateTime(new Date());
        service.update(entity);
        setAttr("entity", entity);
        render("edit.html");
    }

    public void delete() {
        service.deleteByIds(getIds());
        renderJson(Feedback.success());
    }

    public void getParent(){
        Record record = service.getParentEntity(getInt("entityId"));
        renderJson(record);
    }
}
